package com.mianshi;

import java.util.Arrays;

public class bole {
    public static void main(String[] args) {
//        int[] L = {34,23,1,25,75,33,54,8};
//        int k = 60;
//        System.out.println(TwoSum(L, k));

        int[] L = {10,15,20};
        int k = 7;
        System.out.println(cal(L, k));
    }

    public static int TwoSum (int[] L, int K) {
        // write code here
        Arrays.sort(L);
        int ans = -1;
        int len = L.length;
        int sum = 0;
        for(int i = 0; i < len-1; i++){
            for(int j = i+1; j < len; j++){
                sum = L[i] + L[j];
                if(sum > K) break;
                else if(sum == K) return K;
                else ans = Math.max(sum, ans);
            }
        }
        return  ans;
    }

    public static int cal(int[] L, int K){
        Arrays.sort(L);
        int ans = -1;
        int len = L.length;
        int i = 0, j = len -1;
        int sum = 0;
        while(i >= 0 && j < len &&  i < j){
            sum = L[i] + L[j];
            if(sum < K){
                ans = Math.max(ans, sum);
                i++;
            } else {
                j--;
            }
        }
        return ans;
    }
}
